package com.xishu.controller;

import com.xishu.entity.customer.VipRenewEmailTemplate;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.CompanyService;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "续费邮件模式")
@RestController
public class VipRenewEmailTemplateController implements Tools {
    private CompanyService companyService = CompanyService.getInstance();
    private CommonService commonService = CommonService.getInstance();

    @ApiOperation(value = "查询餐厅续费邮件模板")
    @PutMapping(value = ("/vip/renew/template"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody VipRenewEmailTemplate vipRenewEmailTemplate) throws Exception {
        Long companyId = vipRenewEmailTemplate.getCompanyId();
        companyService.findCompanyById(companyId);
        VipRenewEmailTemplate renewEmailTemplate = companyService.findVipRenewEmailTemplate(companyId);

        //没有就创建返回
        if (renewEmailTemplate == null) {
            return commonService.createResponse(companyService.createVipRenewEmailTemplate(companyId));
        }

        //判断stripe是否已经配置好了
        if (getBoolean(renewEmailTemplate.getStripeEnable()) && isNotEmpty(renewEmailTemplate.getStripeAk()) && isNotEmpty(renewEmailTemplate.getStripeSk()) && isNotEmpty(renewEmailTemplate.getStripeWebHookSk())) {
            renewEmailTemplate.setStripeComplete(true);
        }

        //有就返回
        return commonService.createResponse(renewEmailTemplate);
    }

    @ApiOperation(value = "修改餐厅的续费邮件模板")
    @PutMapping(value = ("/vip/renew/template/modify"), produces = ("application/json;charset=UTF-8"))
    public ResponseData modify(@RequestBody VipRenewEmailTemplate vipRenewEmailTemplate) throws Exception {
        Long renewModeId = vipRenewEmailTemplate.getId();
        VipRenewEmailTemplate vipRenewEmailTemplateInDb = commonService.findEntity(renewModeId, VipRenewEmailTemplate.class);
        Long companyId = vipRenewEmailTemplateInDb.getCompanyId();
        boolean own = companyService.ownCompany(companyId);
        VerifyUtil.verify(() -> own);

        //至少要大于等于0，可以当天提醒
        if (vipRenewEmailTemplate.getRemindDay() != null) {
            VerifyUtil.verify(() -> vipRenewEmailTemplate.getRemindDay() >= 0);
        }
        return commonService.modifyObject(vipRenewEmailTemplate);
    }
}
